if (navigator.userAgent.match(/mobile/i)) {
	location.href = "/home/no_mobile";
}
requirejs.config({
	baseUrl: '/static/js',
	paths: {
		jquery: './jquery/jquery-1.11.0.min',
		plugin:'./jquery',
		"jquery.ajax.ext": "./jquery/jquery.ajax.ext",
		stylishSelect:'./jquery/jquery-select-css-style/js/jquery.stylish-select',
		laydate:'./laydate/laydate',
		clipboard:'./clipboard_v2.2.0/ZeroClipboard.min',
		clipboardLess:'./clipboard/ZeroClipboard.min',
		enplaceholder:'./jquery/jquery.enplaceholder',
		trace:"./file_trace",
		text:"./require/text",
		css:"./require/css"
	},
	shim:{
		'laydate': {
			exports:"laydate"
		},
		'plugin/plupload/plupload.dev':{
			deps:['plugin/plupload/moxie']
		},
		'jquery.ajax.ext': {
			deps: ['jquery'],
			exports: '$'
		}
	},
	urlArgs:"v="+UNCLONE_VERSION
});
define(['jquery','comm','space'],function($,comm,Space){
		var upload,
		    files,
		    f_search,
		    folder;
			
		/*var height = $(window).height() - parseInt($('.wf_header').height()-2);
		$(".leftPanel").height(height);*/
		
		var url = window.location.href;

		$.adjustFileListView=function()
		{
			var list = $("#mypage").parent();
			var listTitle = $("#list-title");
			var offset = list.length>0?list[0].offsetWidth - list[0].clientWidth:0;
			if(offset>0)
			{
				listTitle.css("margin-right",offset);
				$("#current-rows-count").css("marginRight",109+offset);
			}
			else
			{
				listTitle.css("margin-right",0);
				$("#current-rows-count").css("marginRight",109);
			}

		}

		if(url.indexOf("files") != -1){
			/*var mypageHeight = height - parseInt($('.tool-view').outerHeight(true)) - parseInt($('.main-title').outerHeight(true))-parseInt($('#list-title').outerHeight(true));
			$("#mypage").height(mypageHeight);*/
			$.adjustFileListView();
		}

		function hackIe11(){
            if(/rv:11\.0/.test(navigator.userAgent))
            {
                var body = $("body")[0];
                if(body.scrollWidth>body.clientWidth)
                {
                    $("#processWindow").css("bottom",17);
                }
                else
                {
                    $("#processWindow").css("bottom",0);
                }
            }
        }
		$(window).resize(function(){
			/*var height = $(window).height() - parseInt($('.wf_header').height() - 2);
			$(".leftPanel").height(height);
			$("#fileInfo").height(height);*/
			
			if(url.indexOf("files") != -1){
				/*var mypageHeight = height - parseInt($('.tool-view').outerHeight(true)) - parseInt($('.main-title').outerHeight(true));
				if($("#list-title").css("display") != "none"){
					mypageHeight = mypageHeight - parseInt($('#list-title').outerHeight(true));
				}
				$("#mypage").height(mypageHeight);*/
				$.adjustFileListView();
			}
            hackIe11();

		});
        hackIe11();
	    $.bindUploadFiles = function(btn)
		{
			btn = btn || "file_add";
			require(["upload/upload"],function(Upload){
				upload = new Upload(btn);
				upload.init();
				$("#upload_form").off("fileUpload.add").on("fileUpload.add",function(e){
					if(!comm.getQueryString("type") && $("#search-detail").is(":hidden") && checkAllFiles()){
						files.add_item(e.file);
						Space.detail();
					}
					else if(url.indexOf("trace") != -1)
					{
						//文件轨迹模块文件上传完之后自动追加到文件轨迹列表
						require(["trace"],function(trace){
							var trace = new trace();
							trace.prependFile(e.file);
						});
					}
				});
				$("#upload_form").off("fileUpload.see").on("fileUpload.see",function(e){
					if(f_search != undefined){
						f_search.forceClose();
					}
					if(!checkAllFiles()){
						window.location.href="/files";
						return;
					}
					if(!comm.getQueryString("type") && !$('#search').data('search')){
						files.listToTop();
					}

				});
			});
		};

		$.bindUploadFiles("file_add");

		if($("#file-empty-upload-btn").length>0)
		{
			$.bindUploadFiles("file-empty-upload-btn");
		}


		function loadNewdir(folder,files)
		{
			folder.container.off("folder.loadNewdir");
			folder.container.off("folder.loadcom");
			folder.container.on("folder.loadNewdir",function(e){
				files.remove();
				folder.get_all(e.file_id,e.name);
			});
			folder.container.on("folder.loadcom",function(e){
				if(e.sortkey != undefined && e.sortkey != null){
					files.sortkey = e.sortkey;
				}
				
				if(e.sortdesc != undefined && e.sortdesc != null){
					files.sortdesc = e.sortdesc;
				}
				//console.log(e);return;
				files.init(true, e.all_data);
			});
		}
		//文件模块
		if(url.indexOf("files") != -1){
			/*var mypageHeight = height - parseInt($('.tool-view').outerHeight(true)) - parseInt($('.main-title').outerHeight(true))-parseInt($('#list-title').outerHeight(true));
			$("#mypage").height(mypageHeight);*/

			require(["files/main","folder/main"],function(Files,Folder){
				files = new Files();
				folder = new Folder(files);

				loadNewdir(folder,files);

				/*$("#mainView").on("folder.bindUI",function(e){

					loadNewdir(folder,files);

				});*/


				$("#wfo").data("last_pid",0);
				//files.initCss();
				//files.init();
				files.container.on("fileList.removeItem",function(e,data){
					upload.pro_stop_animal();
					//console.log(data);
					//Space.update(data);
				});

				files.container.on("fileList.toTop",function(e){
					upload.pro_stop_animal();
					upload.clearPro();
				});
				var folder_id = comm.getHashString("folder_id");
				folder.get_all(folder_id,folder_id,true);
				//folder.first_init();
			});
			require(['search/f_search'],function(F_search){

				f_search = new F_search({target:"#search",container:"#mainView"});
				f_search.init();
//			console.log(f_search.panel);
				f_search.panel.on("filesSearch.search",function(e){
					comm.stopEvent(e);
					f_search.files.fserch_show = true;
					if(checkAllFiles()){
						files.remove();
					}

				});

				f_search.panel.on("filesSearch.close",function(e){
					comm.stopEvent(e);
					$("#wfo").data("last_pid",0);
					if(!checkAllFiles()){
						window.location.href="/files";
					}else{
						f_search.files.fserch_show = false;
						files.remove();
						folder.sortkey = "filecreate";
						folder.sortdesc = "desc";
						folder.isSort = true;
						var last = folder.pathArr.length>0?folder.pathArr[folder.pathArr.length-1]:{uuid:0,name:""};
						folder.get_all(last.uuid,last.name);
						/*var ev = $.Event("folder.loadNewdir");
						 ev.file_id = fd_id;
						 ev.name = fd_name;
						 folder.container.trigger(ev);*/
					}


				});
			});
		}

		if(url.indexOf("trace") != -1){
			require(["trace"],function(trace){
				new trace();
			});
		}

		
		$(document).on('click', function(e){
			function checkExist(id,elem){
				if(id != "processWindow" &&  $(elem).parents("#processWindow").length == 0){
					if(upload != undefined){
						upload.proWindow_close();
					}
					
				}
				
				if(!$(elem).hasClass("wfc") && $(elem).parents(".wfc.item").length == 0){
					
					if(files != undefined){
						var item = $("#fileItem_iconList").parents(".wfc.item");
						if(item.length != 0){
							files.show_mi_icon(false,item);
						}
						files.show_iconList(false);
					}
					
				}
				
				if(id != "search" && $(elem).parents("#search").length == 0){
					if(f_search != undefined){
						f_search.close();
					}
				}
				
				if (elem.id=='js_user_model' || elem.id=='js_downbox') {
					return;
				}
				
			}
			var e = e || window.event; //浏览器兼容性
			var elem = e.target || e.srcElement;
			while (elem) { //循环判断至跟节点，防止点击的是div子元素
				if (elem.id) {
					checkExist(elem.id,elem);
					return;
				}
				elem = elem.parentNode;
			}
			
			if(upload != undefined){
				upload.proWindow_close();
			}
			
			if(files != undefined){
				var item = $("#fileItem_iconList").parents(".wfc.item");
				if(item.length != 0){
					files.show_mi_icon(false,item);
				}
				files.show_iconList(false);
			}
			
			if(f_search != undefined){
				f_search.close();
			}
			
			$('#js_downbox').hide(); //点击的不是div或其子元素
		});
		
		
		
		function checkAllFiles(){
			var type = comm.getQueryString("type");
			var url = window.location.href;
			if(url.indexOf("files") != -1 && type == null){
				return true;
			}
			
			return false;
		}
	
		/**
		 * 选择导航选项
		 */
		function initNavOpt(){
			//console.log($("#nav dl dt"));
			$("#nav dt,#nav dd").click(function(){
				//$(this).find("a").trigger("click");
				var m = $(this);
				var u = $(this).find("a").attr("href");
				/*if(m.is("dd"))
				{
				  m.find(".text_ct").addClass("text_title_hover")
				}*/
				location.href = u;
			});
			/*var url = window.location.href;
			if(url.indexOf("files") != -1){
				var type = comm.getQueryString("type");
				var item = null;
				if(type == null){
					type = 0;
					$("#nav").find("[data-tab]:eq(0)").find(".text_title").addClass(
					"text_title_hover");
				}
				if(type == 1){
					item = $("#nav").find("[data-tab='locked-file']");
					item.find(".text_ct").addClass("text_title_hover");
					$("#folderPath").find(".navtype").html("已下载");
					item.find(".icon-ct").addClass("hover");
				}
				if(type == 2){
					item = $("#nav").find("[data-tab=shareed-file]");
					item.find(".text_ct").addClass("text_title_hover");
					$("#folderPath").find(".navtype").html("已过期");
					item.find(".icon-ct").addClass("hover");
				}
			}else{
				$("#nav").find("[data-tab]:eq(3)").find(".text_title").addClass(
				"text_title_hover");

			}*/


		}

		$('#js_user_model').click(function(){
			$('#js_downbox').show();
		});
		//点击别处时收起下拉框
		$(document).on('click', function(e){
			var e = e || window.event; //浏览器兼容性
			var elem = e.target || e.srcElement;
			while (elem) { //循环判断至跟节点，防止点击的是div子元素
				if (elem.id && (elem.id=='js_user_model')) {
					return;
				}
				elem = elem.parentNode;
			}
			$('#js_downbox').hide(); //点击的不是div或其子元素
		});

		$(document).keydown(function(e){
			var keyEvent;
			if(e.keyCode==8){
				var d=e.srcElement||e.target;
				if(d.tagName.toUpperCase()=='INPUT'||d.tagName.toUpperCase()=='TEXTAREA'){
					keyEvent=d.readOnly||d.disabled;
				}else{
					keyEvent=true;
				}
			}else{
				keyEvent=false;
			}
			if(keyEvent){
				e.preventDefault();
			}
		});

	initNavOpt();

	//获取用户空间
	Space.detail();
});